﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Easily._02_Form._01_frmCtrlConfig
{
    public partial class frmVariableSet : Form
    {
        private GeneralIcon MyIcon = null;
        private string varName = "";//选中变量名
        private string setVar = "";//变量变化值
        private string checkedRadioBtn;//被选中的单选按钮名 


        public frmVariableSet(GeneralIcon icon)
        {
            InitializeComponent();
            checkedRadioBtn = radioBtnAdd.Name;//默认选择增加值
            SetVariables();
            this.MyIcon = icon;
            if (this.MyIcon.code.Save != "")
                this.restore();
            //不选择变量 不可以对其值进行操作
            if (varName == "")
                uiGroupBox.Enabled = false;
            //默认显示
            btn_confirm_Click(new object(), new EventArgs());//触发确认
        }

        /// <summary>
        /// 从保存信息字符串中恢复控件
        /// </summary>
        private void restore()
        {
            string[] tmp = MyIcon.code.Save.Split(';');
            //MyIcon.code.Save = varName + ";" + checkedRadioBtn + ";" + setVar + ";";
            if(tmp.Length==4)
            {
                varName = tmp[0];
                checkedRadioBtn = tmp[1];
                setVar = tmp[2];
                comBox_Var.Text = varName;
                if (checkedRadioBtn == radioBtnAdd.Name)
                {
                    radioBtnAdd.Checked = true;
                    txtAdd.Text = setVar;
                }
                else if (checkedRadioBtn == radioBtnEq.Name)
                {
                    radioBtnEq.Checked = true;
                    txtEq.Text = setVar;
                }
            }
           
        }

        /// <summary>
        /// 设置comBox_Var中的item选项
        /// </summary>
        private void SetVariables()
        {
            //添加可供选择的系统变量到comBox_Var中
            if (Globaldefine.FrmParent.ActivedrmGraphicFlow != null)
            {
                //传感器对应变量
                foreach (string value in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_Sensor_VariableLocal.Values)
                {
                    if (!comBox_Var.Items.Contains(value))
                        comBox_Var.Items.Add(value);
                }
                //全局变量
                foreach (string value in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys)
                {
                    if (!comBox_Var.Items.Contains(value))
                        comBox_Var.Items.Add(value);
                }
                //局部变量
                foreach (string value in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableLocal)
                {
                    if (!comBox_Var.Items.Contains(value))
                        comBox_Var.Items.Add(value);
                }
            }
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            if (checkedRadioBtn == radioBtnAdd.Name)
            {
                //增加0则无意义
                if (setVar != "0")
                    MyIcon.code.InsertCode = "\t" + varName + labAdd.Text + setVar + ";\r";
            }
            else if (checkedRadioBtn == radioBtnEq.Name)
            {
                MyIcon.code.InsertCode = "\t" + varName + labEq.Text + setVar + ";\r";
            }

            MyIcon.code.Save = varName + ";" + checkedRadioBtn + ";" + setVar + ";";//保存控件信息
            //设置注释
            MyIcon.code.Comment = "为变量"+ varName + "赋值";
            //外部会对此进行判断，因此是必要的
            this.DialogResult = DialogResult.OK;
        }

        private void radioBtnAdd_CheckedChanged(object sender, EventArgs e)
        {
            //变量增加是否被选中 跟随单选按钮radioBtnAdd的状态
            uiPanelAdd.Enabled = radioBtnAdd.Checked;
            if (radioBtnAdd.Checked)
                checkedRadioBtn = radioBtnAdd.Name;
        }

        private void radioBtnEq_CheckedChanged(object sender, EventArgs e)
        {
            //变量增加是否被选中 跟随单选按钮radioBtnAdd的状态
            uiPanelEq.Enabled = radioBtnEq.Checked;
            if (radioBtnEq.Checked)
                checkedRadioBtn = radioBtnEq.Name;
        }

        /// <summary>
        /// 获取用户选中的操作变量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comBox_Var_SelectedValueChanged(object sender, EventArgs e)
        {
            if(comBox_Var.Text!="")
            {
                varName = comBox_Var.Text;
                uiGroupBox.Enabled = true;
            }
        }

        private void txtAdd_TextChanged(object sender, EventArgs e)
        {
            setVar = txtAdd.Text;
        }

        private void txtEq_TextChanged(object sender, EventArgs e)
        {
            setVar = txtEq.Text;
        }

        private void btn_comment_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(this.MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                    this.MyIcon.code.Comment = NE.NoteEdit;
            }
        }
    }
}
